package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class DownloadEventPeer extends BasePeer{
	public static final String tableName = "DownloadEvent";
	public static final String EVENTID = "eventId";
	public static final String DOWNLOADERID = "downloaderId";
	public static final String UPLOADID = "uploadId";
	public static final String DOWNLOADTIME = "downloadTime";
	public static final String SUCCEED = "succeed";
	public static final String COSTSECONDS = "costSeconds";
	public static final String[] columnNames=new String[]{"eventId","downloaderId","uploadId","downloadTime","succeed","costSeconds"};
	public static DownloadEvent getDownloadEventByPKs(Object eventId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(DownloadEventPeer.tableName,DownloadEventPeer.columnNames," TRUE  AND `eventId`=? ",new Object[]{eventId,},0, 1,conn);
			DownloadEvent downloadEvent = new DownloadEvent();
		fill(rs,downloadEvent);
		return downloadEvent;
	}
	public static DownloadEvent getDownloadEventReferenceColumnsOnlyByPKs(Object eventId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(DownloadEventPeer.tableName,new String[]{"eventId",}," TRUE AND `eventId`=?",new Object[]{eventId,}, 0,  1,conn);
			DownloadEvent downloadEvent = new DownloadEvent();
		fill(rs,downloadEvent);
		return downloadEvent;
	}
	public static DownloadEvent getDownloadEventByEventId(long eventId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(DownloadEventPeer.tableName,DownloadEventPeer.columnNames,"`eventId`=?",new Object[]{eventId},0, 1,conn);
			DownloadEvent downloadEvent = new DownloadEvent();
		fill(rs,downloadEvent);
		return downloadEvent;
	}
	public static void fill(ResultSet rs, DownloadEvent downloadEvent) throws SQLException {
		if(rs.next()){
			try{downloadEvent.setEventId(rs.getLong("eventId"));}catch(Exception ex){}
			try{downloadEvent.setDownloaderId(rs.getLong("downloaderId"));}catch(Exception ex){}
			try{downloadEvent.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{downloadEvent.setDownloadTime(rs.getTimestamp("downloadTime"));}catch(Exception ex){}
			try{downloadEvent.setSucceed(rs.getBoolean("succeed"));}catch(Exception ex){}
			try{downloadEvent.setCostSeconds(rs.getLong("costSeconds"));}catch(Exception ex){}
			downloadEvent.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<DownloadEvent> downloadEventCollection) throws SQLException {
		while(rs.next()){
			DownloadEvent downloadEvent=new DownloadEvent();
			try{downloadEvent.setEventId(rs.getLong("eventId"));}catch(Exception ex){}
			try{downloadEvent.setDownloaderId(rs.getLong("downloaderId"));}catch(Exception ex){}
			try{downloadEvent.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{downloadEvent.setDownloadTime(rs.getTimestamp("downloadTime"));}catch(Exception ex){}
			try{downloadEvent.setSucceed(rs.getBoolean("succeed"));}catch(Exception ex){}
			try{downloadEvent.setCostSeconds(rs.getLong("costSeconds"));}catch(Exception ex){}
			downloadEvent.resetModified();
			downloadEventCollection.add(downloadEvent);
		}
	}
	public static boolean update(DownloadEvent downloadEvent, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < DownloadEventPeer.columnNames.length; i++) {
			if (((DownloadEventBase)downloadEvent).modified[i]) {
				columns.add(DownloadEventPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `DownloadEvent` SET ");
		sql.append("WHERE TRUE AND `eventId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,downloadEvent.get(column));
		}
		pstmt.setObject(columns.size()+1+0,downloadEvent.get("eventId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(DownloadEvent downloadEvent, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (downloadEvent.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `DownloadEvent` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (downloadEvent.get(column) != null) {
				pstmt.setObject(i + 1 + j, downloadEvent.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<DownloadEvent> getDownloadEventCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<DownloadEvent> collection=new Vector<DownloadEvent>();
		fill(rs,collection);
		return collection;
	}
}
